<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs2.model.*, bbs2.dao.*, bbs2.service.*, bbs2.util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="../_checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="../_header.jsp" %>  
<style>
    input[type=file] { width: 500px; }
    div.container table { width: auto; margin-bottom: 0;}
    table tr:first-child { background-color: #eee; }
    table td:nth-child(1) { min-width: 400px; }
    table td:nth-child(2) { text-align: right; min-width: 80px; }
    table td:nth-child(3) { text-align: center; }
    table tr:last-child { background-color: #eee; }
</style>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int targetType = param.getInt("targetType", 0);
    int targetId = param.getInt("targetId", 0);
    String cmd = param.getString("cmd", "");
    
    if (cmd.equals("delete")) {
        int id = param.getInt("id", 0);        
        FileDAO.delete(id);
    }    
    ArrayList<File> list = FileDAO.select(targetType, targetId); 
    
    String returnUrl = "/bbs2/file/upload_list.jsp?targetType=" + targetType + "&targetId=" + targetId;
%>
<script>
    function resize_parent_iframe() {
        if (window.parent) {
            var h = $("html").height();
            $('iframe.file', window.parent.document).css("height", h);
        }
    }
    $(resize_parent_iframe);
</script>
<body>
<div class="container">

        <table class="table table-bordered table-condensed">
            <% if (list.size() > 0) { %>
                <tr><td>첨부파일</td><td>파일크기</td><td></td></tr>
            <% } %>
            <% for (File file : list) { %>
                <tr>
                    <td><%= file.getFileName() %></td>
                    <td><%= file.getFileSize() / 1024 %> kb</td>
                    <td>
                        <form method="post">
                            <input type="hidden" name="id" value="<%= file.getId() %>">
                            <input type="hidden" name="targetType" value="<%= targetType %>" />
                            <input type="hidden" name="targetId" value="<%= targetId %>" />
                            <button type="submit" class="btn btn-mini" name="cmd" value="delete" 
                                    onclick="return confirm('삭제하시겠습니까?')">삭제</button>
                            <a class="btn btn-mini" href="/bbs2/FileDownloadServlet?id=<%= file.getId() %>">다운로드</a>
                        </form>    
                    </td>                        
                </tr>    
            <% } %>    
            <tr>
                <td colspan="3">
                    <form method="post" action="/bbs2/FileUploadServlet" enctype="multipart/form-data">
                        <input type="hidden" name="targetType" value="<%= targetType %>" />
                        <input type="hidden" name="targetId" value="<%= targetId %>" />
                        <input type="hidden" name="returnUrl" value="<%= returnUrl %>" />
                        <input type="file" name="file" />
                        <button type="submit" class="btn btn-small">파일 업로드</button>
                    </form>
                </td>                    
            </tr>
        </table>
        
</div>
</body>
